<script>
	import Vue from 'vue'
	export default {
		methods:{
			initialData(){
				this.initDictionary()
				this.initCategory()
			},
			//初始化字典数据，在storage里
			async initDictionary() {
			    let res = await this.$api.getDictionary();
				uni.setStorageSync("dictionary", res.data.data);
			},
			async initCategory(){
				let res = await this.$api.getCategories();
				uni.setStorageSync("tabBars", res.data.data);
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			uni.getSystemInfo({
				success: function(e) {
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
				}
			})
		},
		onShow: function() {
			//处理token与 userinfo
			let token = uni.getStorageSync('token')
			let userInfo = uni.getStorageSync("userInfo")
			if(!token){
				
				let that = this
				uni.login({
					success(loginRes) {
						that.$api.getSessionKey({code: loginRes.code}).then((loginValue)=>{
							console.log('get new token :', loginValue.data.data)
							uni.setStorageSync('token', loginValue.data.data)
							that.initialData()
							// 获取token成功后判断是否有微信userinfo，没有跳转
							if(!userInfo.nickName){
								uni.navigateTo({
									url: '/pages/user/user'
								})
							}else{
								uni.reLaunch({
									url: '/pages/main/main'
								})
							}
						}).catch((err)=>{
							console.log('request fail', err);
						})
					}
				})
			}else{
				console.log('token')
				this.initialData()
				if(!userInfo.nickName){
					uni.navigateTo({
						url: '/pages/user/user'
					})
				}
			}
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import './common/uni.css';
	
</style>
